//地址联动
$(function(){
	var box = $('.js-select-address-box')
	var tpl = $('.js-select-address').remove()
	var formName  = "address_id"; 
	var optionTpl = $("<option></option>");
	var currentId = 0;
	function getSonIds(){
		api.solo('area',[currentId],function(d){
			var list = d.data.list;
			var length = list.length;
			addName()
			if(length<1) return;
			var newSelect = tpl.clone()
			for(var i=0;i<length;i++){
				var thisOption = optionTpl.clone();
				thisOption.val(list[i]['id']).html(list[i]['name'])
				newSelect.append(thisOption)
			}
			//特殊设置
			if(currentId==0) addNoAddress(newSelect)
			box.append(newSelect)
			currentId = list[0]['id']
			getSonIds()
		})
	}
	function addName(){
		box.find('.js-select-address').removeAttr('name');
		box.find('.js-select-address:last').attr('name', formName);
	}
	//特殊化 第一条
	function addNoAddress(that){
		var specialOption = optionTpl.clone().val(-1).html('其他地区')
		that.append(specialOption)
	}

	//初始化
	getSonIds()
	box.on('change', '.js-select-address', function(event) {
		console.log('change')
		currentId = $(this).val();
		$(this).nextAll().remove()
		getSonIds()
	});
})